<template>
	<div class="system-group-container">
		<el-dialog title="添加用户" v-model="isShowDialog" width="80%">
			<el-form ref="addUserGroupFormRef" :model="formData" :rules="formRules" label-width="80px">
				<el-card shadow="hover">
					<el-form :inline="true" :model="search">
						<el-form-item>
							<el-input v-model="search.loginName" placeholder="请输入登录账号" prefix-icon="el-icon-search"></el-input>
						</el-form-item>
						<el-form-item>
							<el-input v-model="search.userName" placeholder="请输入用户名称" prefix-icon="el-icon-search"></el-input>
						</el-form-item>
						<el-form-item>
							<el-date-picker type="daterange" align="right" v-model="dateData.value" :shortcuts="dateData.shortcuts"
								unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
						</el-form-item>
						<el-form-item>
							<el-button icon="el-icon-search" type="primary" @click="onTableSearch()">查询</el-button>
							<el-button icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
						</el-form-item>
						<el-form-item>
							<el-button v-auth="'system:user:remove'" type="primary" icon="el-icon-document-add" @click="onAddUserGroup()" :disabled="tableData.selections.length <= 0">添加</el-button>
						</el-form-item>
					</el-form>
					<el-table ref="tableUserRef" :data="tableData.data" @selection-change="onTableSelectionChange" style="width: 100%" row-key="id">
						<el-table-column type="selection" align="cneter" width="50" show-overflow-tooltip></el-table-column>
						<el-table-column type="index" align="cneter" width="50" show-overflow-tooltip></el-table-column>
						<!-- <el-table-column label="头像" width="50" show-overflow-tooltip>
							<template #default="scope">
								<el-image style="width: 30px; height: 30px"
									v-if="scope.row.avatar"
									:src="scope.row.avatar" 
									:preview-src-list="[scope.row.avatar]">
								</el-image>
								<el-image style="width: 30px; height: 30px"
									v-else
									src="/images/profile.png" 
									preview-src-list="['/images/profile.png']">
								</el-image>
							</template>
						</el-table-column> -->
						<el-table-column label="登录账号" prop="loginName" show-overflow-tooltip></el-table-column>
						<el-table-column label="用户名称" prop="userName" show-overflow-tooltip></el-table-column>
						<el-table-column label="性别" prop="sex" width="60" align="center" show-overflow-tooltip>
							<template #default="scope">
								<span v-if="scope.row.sex == '0'" class="color-primary">男</span>
								<span v-else-if="scope.row.sex == '1'" class="color-success">女</span>
								<span v-else class="color-info">未知</span>
							</template>
						</el-table-column>
						<el-table-column label="手机号码" prop="phone" show-overflow-tooltip></el-table-column>
						<el-table-column label="邮箱账号" prop="email" show-overflow-tooltip></el-table-column>
						<el-table-column label="状态" show-overflow-tooltip width="70">
							<template #default="scope">
								<span v-if="scope.row.status == '0'" class="color-primary">正常</span>
								<span v-else-if="scope.row.status == '1'" class="color-info">停用</span>
								<span v-else class="color-danger">删除</span>
							</template>
						</el-table-column>
						<el-table-column label="创建时间" prop="createTime" width="180" show-overflow-tooltip></el-table-column>
						<el-table-column label="操作" show-overflow-tooltip width="80"
							v-auths="['group:user:add']">
							<template #default="scope">
								<el-button v-auth="'group:user:add'" type="primary" link @click="onAddUserGroup(scope.row)">添加</el-button>
							</template>
						</el-table-column>
					</el-table>
					<el-pagination
						@size-change="onPageSizeChange"
						@current-change="onPageCurrentChange"
						class="mt15"
						:pager-count="5"
						:page-sizes="[10, 20, 30, 40, 50, 60]"
						v-model:current-page="search.pageNum"
						background
						v-model:page-size="search.pageSize"
						layout="total, sizes, prev, pager, next, jumper"
						:total="tableData.total"
					>
					</el-pagination>
				</el-card>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel">取 消</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { getUserList } from '/@/api/system/user/index';
import { addUserGroup } from '/@/api/system/group/index';
import { formatDate, getDateRangeShortcuts } from '/@/utils/formatTime';
export default {
	name: 'group/user/add',
	setup(props, context) {
		const tableUserRef = ref();
		const addUserGroupFormRef = ref();
		const state = reactive({
			isShowDialog: false,
			formData: {},
			search: {
				groupIds: [],
				allocated: 'N',//已分配的用户
				loginName: undefined,
				userName: undefined,
				startTime: undefined,
				endTime: undefined,
				orderBy: 'createTime',
				isAsc: 'desc',
				pageNum: 1,
				pageSize: 10
			},
			dateData: {
				value: [],
				shortcuts: getDateRangeShortcuts()
			},
			tableData: {
				selections: [],//选中的行数据
				data: [],
				total: 0,
				loading: false
			}
		});
		
		const loadTableData = (params) => {
			getUserList(Object.assign(state.search, params)).then((res) => {
				if(res.code == 0){
					state.tableData.data = res.data;
					state.tableData.total = res.total;
					tableUserRef.value.clearSelection();
				}
			});
		}
		
		//查询
		const onTableSearch = () => {
			if(state.dateData.value.length == 2){
				state.search.startTime = formatDate(state.dateData.value[0], 'yyyy-MM-dd') + ' 00:00:00';
				state.search.endTime = formatDate(state.dateData.value[1], 'yyyy-MM-dd') + ' 23:59:59';
			}
			loadTableData(state.search);
		}
		//重置
		const onResetSearch = () => {
			state.dateData.value = [];
			state.search.groupIds = [];
			state.search.loginName = undefined;
			state.search.userName = undefined;
			state.search.startTime = undefined;
			state.search.endTime = undefined;
			state.search.pageNum = 1;
			state.search.pageSize = 10;
			loadTableData(state.search);
		}
		// 监听表格选中行
		const onTableSelectionChange = (selections) =>{
			state.tableData.selections = selections;
		};
		// 每页显示数据大小改变
		const onPageSizeChange = (val: number) => {
			state.search.pageSize = val;
			loadTableData(state.search);
		};
		// 页码改变
		const onPageCurrentChange = (val: number) => {
			state.search.pageNum = val;
			loadTableData(state.search);
		};
		// 添加用户
		const onAddUserGroup = (row: object) => {
			const userIds = row ? [row.id] : state.tableData.selections.map(item => item.id);
			let params = {
				userIds: userIds,
				groupId: state.search.groupIds.length > 0 ? state.search.groupIds[0] : '',
			}
			addUserGroup(params).then((res) => {
				if(res.code === 0){
					ElMessage.success('添加成功');
					onTableSearch();
				}
			});
		}
		// 打开弹窗
		const openDialog = (row: any) => {
			state.isShowDialog = true;
			if(row){
				state.search.groupIds.push(row.groupId);
				loadTableData();
			}
		};
		// 关闭弹窗
		const closeDialog = (row?: object) => {
			state.isShowDialog = false;
			for(var key in state.formData) {
				state.formData[key] = undefined;
			}
			addUserGroupFormRef.value.resetFields();
		};
		// 取消
		const onCancel = () => {
			context.emit('submit');
			closeDialog();
		};
		
		return {
			tableUserRef,
			addUserGroupFormRef,
			onTableSearch,
			onTableSelectionChange,
			onResetSearch,
			onPageSizeChange,
			onPageCurrentChange,
			onAddUserGroup,
			openDialog,
			closeDialog,
			onCancel,
			...toRefs(state),
		};
	},
};
</script>
